package com.aprilTomCat.conf;

import com.aprilTomCat.Servlet.HttpServlet;
import com.aprilTomCat.util.SearchClassUtil;
import com.aprilTomCat.webServlet.WebServlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author April
 *
 */
public class ServletConfigMapping {
    //定义servlet容器
    public static Map<String, Class<HttpServlet>> classMap=new HashMap();

    //将每一个自定义servlet的信息放入ServletConfig中
    private static List<ServletConfig> configs=new ArrayList();

    static {
        //1.获取用户自定义的Servlet的全路径名
        List<String> classPaths= SearchClassUtil.searchClass();
        for(String classPath:classPaths){
            try {
                System.out.println(classPath);
                getMessage(classPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    public static void getMessage(String classPath) throws  Exception {
        Class  clazz = Class.forName(classPath);
        //获取注解信息
        WebServlet webServlet = (WebServlet) clazz.getDeclaredAnnotation(WebServlet.class);

        //需要将解析的信息加载到configs结合当中----保证：url和类对象是相同的
        configs.add(new ServletConfig(webServlet.urlMapping(), classPath));
    }
    //将url和类对象翻入Servlet容器
    public static void initServlet() throws ClassNotFoundException {
        System.out.println("****//");
        for (ServletConfig config:configs) {
            classMap.put(config.getUrlMapping(), (Class<HttpServlet>) Class.forName(config.getClassPath()));
        }
    }

}
